package com.lagou.rabbit.demo.controller;

import com.lagou.rabbit.demo.po.Order;
import com.lagou.rabbit.demo.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class OrderController {

    @Autowired
    private OrderService orderService;


    @RequestMapping("/order/{userId}/{courseId}")
    public String saveOrder(
            @PathVariable("userId") Integer userId,
            @PathVariable("courseId") Integer courseId
    ) {

        Order order = new Order();
        order.setUserId(userId);
        order.setCourseId(courseId);

        orderService.saveOrder(order);

        return "redirect:/page/success/" + order.getOrderNo();
    }


    @RequestMapping("/order/list/{userId}")
    public String showOrderList(
            @PathVariable("userId") Integer userId,
            Model model
    ) {
        List<Order> orders = orderService.listOrders(userId);
        model.addAttribute("orderList", orders);
        return "order_list";
    }

}
